package linklist;

/**
 * @author: liuchong
 * @create: 2023/3/13 21:57
 * @description:
 */
//Definition for singly-linked list.
class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) {
        this.val = val;
    }
    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}
public class MergeTwoList_21 {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1 == null) return list2;
        if(list2 == null) return list1;
        ListNode resultNode = new ListNode();
        ListNode p = resultNode;
        while (list1 != null && list2 != null){
            if(list1.val < list2.val) {
                p.next = list1;
                list1 = list1.next;
            } else {
                p.next = list2;
                list2 = list2.next;
            }
            p = p.next;
        }
        if(list1 != null) p.next = list1;
        if(list2 != null) p.next = list2;
        return  resultNode.next;
    }
}
